1 Setup

1.1 Packages

library(tidyverse)
-- Attaching packages --------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
v ggplot2 3.1.0     v purrr   0.2.5
v tibble  1.4.2     v dplyr   0.7.8
v tidyr   0.8.2     v stringr 1.3.1
v readr   1.1.1     v forcats 0.3.0
-- Conflicts ------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(cowplot)

Attaching package: 'cowplot'
The following object is masked from 'package:ggplot2':

    ggsave
library(GGally)

Attaching package: 'GGally'
The following object is masked from 'package:dplyr':

    nasa
library(heatmaply)
Loading required package: plotly

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
Loading required package: viridis
Loading required package: viridisLite

======================
Welcome to heatmaply version 0.15.2

Type citation('heatmaply') for how to cite the package.
Type ?heatmaply for the main documentation.

The github page is: https://github.com/talgalili/heatmaply/
Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
Or contact: <tal.galili@gmail.com>
======================
library(sva)
Loading required package: mgcv
Loading required package: nlme

Attaching package: 'nlme'
The following object is masked from 'package:dplyr':

    collapse
This is mgcv 1.8-25. For overview type 'help("mgcv-package")'.
Loading required package: genefilter

Attaching package: 'genefilter'
The following object is masked from 'package:readr':

    spec
Loading required package: BiocParallel
library(limma)
library(biobroom)
Loading required package: broom
library(ggridges)

Attaching package: 'ggridges'
The following object is masked from 'package:ggplot2':

    scale_discrete_manual

1.2 Data

1.2.1 Metabolite Abundances

# Cells #
vf.cell.neg.raw <- read_csv("./data/abundances/p5fa_vpa_exp_hilic_target_cells_negmode_abundances.csv")
Parsed with column specification:
cols(
  .default = col_double(),
  Samples = col_character(),
  Mode = col_character(),
  Type = col_character(),
  Group = col_character(),
  VPA = col_character(),
  FA = col_character(),
  Plate = col_character()
)
See spec(...) for full column specifications.
vf.cell.pos.raw <- read_csv("./data/abundances/p5fa_vpa_exp_hilic_target_cells_posmode_abundances.csv")
Parsed with column specification:
cols(
  .default = col_double(),
  Samples = col_character(),
  Mode = col_character(),
  Type = col_character(),
  Group = col_character(),
  VPA = col_character(),
  FA = col_character(),
  Plate = col_character()
)
See spec(...) for full column specifications.
# Media #
vf.med.neg.raw <- read_csv("./data/abundances/p5fa_vpa_exp_hilic_target_media_negmode_abundances.csv")
Parsed with column specification:
cols(
  .default = col_double(),
  Samples = col_character(),
  Mode = col_character(),
  Type = col_character(),
  Group = col_character(),
  VPA = col_character(),
  FA = col_character(),
  Plate = col_character()
)
See spec(...) for full column specifications.
vf.med.pos.raw <- read_csv("./data/abundances/p5fa_vpa_exp_hilic_target_media_posmode_abundances.csv")
Parsed with column specification:
cols(
  .default = col_double(),
  Samples = col_character(),
  Mode = col_character(),
  Type = col_character(),
  Group = col_character(),
  VPA = col_character(),
  FA = col_character(),
  Plate = col_character()
)
See spec(...) for full column specifications.

1.2.2 Compound Information

# Cells #
vf.cell.neg.compound.info <- read_csv("./data/compound_info/p5fa_vpa_exp_hilic_target_cells_negmode_cmpnd_info.csv")
Parsed with column specification:
cols(
  compound_short = col_character(),
  compound_full = col_character(),
  formula = col_character(),
  mass = col_double(),
  rt = col_double(),
  cas_id = col_character()
)
vf.cell.pos.compound.info <- read_csv("./data/compound_info/p5fa_vpa_exp_hilic_target_cells_posmode_cmpnd_info.csv")
Parsed with column specification:
cols(
  compound_short = col_character(),
  compound_full = col_character(),
  formula = col_character(),
  mass = col_double(),
  rt = col_double(),
  cas_id = col_character()
)
# Media #
vf.med.neg.compound.info <- read_csv("./data/compound_info/p5fa_vpa_exp_hilic_target_media_negmode_cmpnd_info.csv")
Parsed with column specification:
cols(
  compound_short = col_character(),
  compound_full = col_character(),
  formula = col_character(),
  mass = col_double(),
  rt = col_double(),
  cas_id = col_character()
)
vf.med.pos.compound.info <- read_csv("./data/compound_info/p5fa_vpa_exp_hilic_target_media_posmode_cmpnd_info.csv")
Parsed with column specification:
cols(
  compound_short = col_character(),
  compound_full = col_character(),
  formula = col_character(),
  mass = col_double(),
  rt = col_double(),
  cas_id = col_character()
)
# Kegg/other ID reference #
#cmpnd.id.db <- read_csv("./data/anp_db_compound_kegg.csv")

1.3 Functions

MissingPerSamplePlot <- function(raw.data, start.string) {
  # Counts the number of missing/NA values per sample and
  # percent compounds missing out of total number of compounds per sample
  # Then passes the result into a vertical bar plot, where each 
  # bar represents a single sample and the size of the bar 
  # is the % of compounds missing
  
  counted.na <- raw.data %>%
  select(starts_with(start.string)) %>% 
  mutate(
    count.na = apply(., 1, function(x) sum(is.na(x))),
    percent.na = (count.na / ncol(raw.data %>% select(starts_with(start.string)))) * 100
    ) %>%
  dplyr::select(count.na, percent.na) %>%
  bind_cols(
    raw.data %>% 
      select(Samples, Group)
      ) %>% 
  arrange(percent.na) %>% 
  mutate(f.order = factor(Samples, levels = Samples))
counted.na %>% 
  ggplot(aes(x = f.order, y = percent.na, fill = Group)) +
  geom_bar(stat = "identity") +
  geom_hline(yintercept = 20, color = "gray", size = 1, alpha = 0.8) +
  coord_flip()+
  xlab("Samples") +
  ylab("Percent missing values in sample") +
  theme(axis.text.y = element_text(size = 6)) 
}
MissingPerCompound <- function(raw.data, start.string){
  # Function to count in how many experimental samples each compound is missing
  # Also calculates the % missing:
  # (# NA per compound across all experimental samples) * 100 / (tot num of samples)
  
  raw.data %>% 
  filter(Group == "sample") %>% 
  select(Samples, starts_with(start.string)) %>% 
  gather(key = "Compound", value = "Abundance", -Samples) %>% 
  group_by(Compound) %>% 
  summarise(
    na_count = sum(is.na(Abundance)),
    n_samples = n(),
    percent_na = (na_count * 100 / n_samples)
    ) %>% 
  filter(na_count > 0) %>% 
  arrange(desc(percent_na))
}
ReplaceNAwMinLogTransformMult <- function(raw.dataframe, start.prefix) {
  # Function to replace any NAs in each column with the minimum for that column, 
  # separately for each sample type.
  # NA in negative control samples are replaced by 2.
  # Then data is log2 transformed
  
  # experimental samples #
  smpls <- raw.dataframe %>%
    filter(Group == "sample") %>% 
    dplyr::select(starts_with(start.prefix))
  smpls.min <- lapply(smpls, min, na.rm = TRUE)
  smpls.noNA <- raw.dataframe  %>%
    filter(Group == "sample") %>% 
    dplyr::select(Samples:Plate) %>%
    bind_cols(
      smpls %>%
        replace_na(replace = smpls.min) %>% 
        log2()
      ) 
  # QC samples #
  QC <- raw.dataframe %>%
    filter(Group == "mix") %>% 
    dplyr::select(starts_with(start.prefix))
  QC.min <- lapply(QC, min, na.rm = TRUE)
  # replace the missing values in the QC with the minimum of the QC
  # then take the log
  QC.noNA <- raw.dataframe  %>%
    filter(Group == "mix") %>% 
    dplyr::select(Samples:Plate) %>%
    bind_cols(
      QC %>%
        replace_na(replace = QC.min) %>% 
        log2()
      )
  # not samples or QC #
  other.min <- setNames(
    as.list(
      rep(2, ncol(
        raw.dataframe %>% 
          dplyr::select(starts_with(start.prefix))))
      ),
    colnames(raw.dataframe %>% dplyr::select(starts_with(start.prefix)))
                )
  other.num.log <- raw.dataframe  %>%
    filter(Group != "mix" & Group != "sample") %>% 
    dplyr::select(Samples:Plate) %>%
    bind_cols(
      raw.dataframe %>% 
        filter(Group != "mix" & Group != "sample") %>% 
        dplyr::select(starts_with(start.prefix)) %>%
        replace_na(replace = other.min) %>% 
        log2()
      )
  all.noNA <- smpls.noNA %>% 
    bind_rows(QC.noNA) %>% 
    bind_rows(other.num.log)
}
HeatmapPrepAlt <- function(raw.data, start.prefix){
  # function for preparing dara for heatmap viz
  x <- raw.data %>% 
    select(starts_with(start.prefix)) %>% 
    scale(center = TRUE, scale = TRUE) %>% 
    as.matrix() 
  row.names(x) <- raw.data$Samples
  return(x)
}

2 Data Exploration

2.1 Mass vs Retention Time

Q: What are the distributions of compound masses and retention times?

full.vf.cmpnd <- vf.cell.neg.compound.info %>% 
  mutate(Set = "Cells / Neg") %>% 
  bind_rows(vf.cell.pos.compound.info %>% mutate(Set = "Cells / Pos")) %>% 
  bind_rows(vf.med.neg.compound.info %>% mutate(Set = "Media / Neg")) %>%
  bind_rows(vf.med.pos.compound.info %>% mutate(Set = "Media / Pos"))
full.vf.cmpnd %>% 
  ggplot(aes(x = rt, y = mass)) +
  geom_point(size = 3, alpha = 0.3) +
  xlab("Retention Time (min)") +
  ylab("Mass (Da)") +
  ggtitle("Mass v RT\nVPA + FA HILIC") +
  ylim(0, 1000)

full.vf.cmpnd %>% 
  ggplot(aes(x = rt, y = mass, color = Set)) +
  geom_point(size = 3, alpha = 0.8) +
  xlab("Retnetion Time (min)") +
  ylab("Mass (Da)") +
  ggtitle("Mass v RT\nVPA + FA HILIC") +
  facet_wrap(~ Set) +
  ylim(0, 1000)

Q: Which compounds were found in one or more of the data types?

## cell join ##
vf.cell.cmpnd.join <- vf.cell.neg.compound.info %>% 
  inner_join(vf.cell.pos.compound.info, by = "cas_id", suffix = c(".c.n", ".c.p")) %>% 
  select(
    contains("cas_id"), contains("short"), 
    contains("full"), starts_with("formula"), 
    starts_with("mass"), starts_with("rt")
    )
# compound names - found in pos and neg mode / cells 
print(vf.cell.cmpnd.join$compound_full.c.n)
 [1] "Glycine"                                          
 [2] "Pyruvate"                                         
 [3] "Alanine"                                          
 [4] "Beta-Alanine"                                     
 [5] "Sarcosine"                                        
 [6] "2-Aminobutyric Acid"                              
 [7] "BAIBA"                                            
 [8] "GABA"                                             
 [9] "Serine"                                           
[10] "Hypotaurine"                                      
[11] "Uracil"                                           
[12] "Creatinine"                                       
[13] "Proline"                                          
[14] "Valine"                                           
[15] "Threonine"                                        
[16] "Homoserine"                                       
[17] "Taurine"                                          
[18] "Ketoleucine"                                      
[19] "N-Acetylalanine"                                  
[20] "Creatine"                                         
[21] "Leucine"                                          
[22] "Isoleucine"                                       
[23] "Asparagine"                                       
[24] "Ornithine"                                        
[25] "Aspartic Acid"                                    
[26] "Adenine"                                          
[27] "Glutamine"                                        
[28] "Lysine"                                           
[29] "Glutamic Acid"                                    
[30] "Methionine"                                       
[31] "Xanthine"                                         
[32] "4-Hydroxyphenylacetic Acid"                       
[33] "3-Sulfinoalanine"                                 
[34] "Histidine"                                        
[35] "Allantoin"                                        
[36] "5-Hydroxylysine"                                  
[37] "Phenylalanine"                                    
[38] "Pyridoxal"                                        
[39] "Pyridoxine"                                       
[40] "Glycerol 2-Phosphate"                             
[41] "Arginine"                                         
[42] "Tyrosine"                                         
[43] "D-Galactitol"                                     
[44] "D-Sorbitol"                                       
[45] "Phosphocholine"                                   
[46] "N-alpha-Acetyl-L-glutamine"                       
[47] "Tryptophan"                                       
[48] "Pantothenic Acid"                                 
[49] "Cystathionine"                                    
[50] "Methyl Jasmonate"                                 
[51] "Carnosine"                                        
[52] "Cytidine"                                         
[53] "Uridine"                                          
[54] "D-Glucose 6-phosphate"                            
[55] "D-Fructose 6-phosphate"                           
[56] "Thiamine (Vit B1)"                                
[57] "Inosine"                                          
[58] "Guanosine"                                        
[59] "Ophthalmic Acid"                                  
[60] "5'-Methylthioadenosine"                           
[61] "N-Acetylaspartyl Glutamic Acid"                   
[62] "Glutathione (GSH)"                                
[63] "N-Acetylneuraminic Acid"                          
[64] "UMP"                                              
[65] "3-Phosphoglyceroinositol"                         
[66] "AMP"                                              
[67] "S-Adenosylhomocysteine (SAH)"                     
[68] "CDP"                                              
[69] "ADP"                                              
[70] "GDP"                                              
[71] "UTP"                                              
[72] "ATP"                                              
[73] "GTP"                                              
[74] "Cyclic adenosine diphosphate ribose (cADP-ribose)"
[75] "UDP-N-Acetylgalactosamine"                        
[76] "GSSG"                                             
[77] "NAD"                                              
[78] "NADH"                                             
[79] "NADP"                                             
[80] "Flavin adenine dinucleotide (FAD)"                
[81] "Acetyl-CoA"                                       
# percent of cell / neg compounds found in cell / pos 
round(nrow(vf.cell.cmpnd.join) * 100 / nrow(vf.cell.neg.compound.info), 1)
[1] 58.3
# percent of cell / neg compounds found in cell / pos 
round(nrow(vf.cell.cmpnd.join) * 100 / nrow(vf.cell.pos.compound.info), 1)
[1] 56.2
vf.cell.cmpnd.join %>% 
  select(contains("mass")) %>% 
  ggpairs()

vf.cell.cmpnd.join %>% 
  select(starts_with("rt")) %>% 
  ggpairs()

### Media join ###
vf.med.cmpnd.join <- vf.med.neg.compound.info %>% 
  inner_join(vf.med.pos.compound.info, by = "cas_id", suffix = c(".m.n", ".m.p")) %>% 
  select(
    contains("cas_id"), contains("short"), 
    contains("full"), starts_with("formula"), 
    starts_with("mass"), starts_with("rt")
    )
# compound names - found in pos and neg mode / media
print(vf.med.cmpnd.join$compound_full.m.n)
 [1] "Alanine"           "Serine"            "Creatinine"       
 [4] "Proline"           "Valine"            "Threonine"        
 [7] "Taurine"           "Creatine"          "Leucine"          
[10] "Isoleucine"        "Glutamine"         "Lysine"           
[13] "Glutamic Acid"     "Methionine"        "Histidine"        
[16] "Allantoin"         "Phenylalanine"     "Pyridoxine"       
[19] "Arginine"          "Citrulline"        "Tyrosine"         
[22] "D-Sorbitol"        "Tryptophan"        "Pantothenic Acid" 
[25] "Thiamine (Vit B1)"
# percent of media / neg compounds found in media / pos
round(nrow(vf.med.cmpnd.join) * 100 / nrow(vf.med.neg.compound.info), 1)
[1] 44.6
# percent of media / pos compounds found in media / neg
round(nrow(vf.med.cmpnd.join) * 100 / nrow(vf.med.pos.compound.info), 1)
[1] 53.2
# vf all match
vf.all.cmpnd.join <- vf.cell.cmpnd.join %>% 
  inner_join(vf.med.cmpnd.join, by = "cas_id") %>% 
  select(
    contains("cas_id"), contains("short"), 
    contains("full"), starts_with("formula"), 
    starts_with("mass"), starts_with("rt")
    )
nrow(vf.all.cmpnd.join)
[1] 24
vf.all.cmpnd.join %>% 
  select(contains("mass")) %>% 
  ggpairs()

vf.all.cmpnd.join %>% 
  select(starts_with("rt")) %>% 
  ggpairs()

2.2 Missing Values

Q: Do any of the samples have greater than 20% missing (NA) compound abundances, out of all of the features in the dataset?

MissingPerSamplePlot(vf.cell.neg.raw, "hVPA_FAnC") +
  ggtitle("Missing Per Sample\nVPA + FA HILIC / Cells / Neg Mode")

MissingPerSamplePlot(vf.cell.pos.raw, "hVPA_FApC") +
  ggtitle("Missing Per Sample\nVPA + FA HILIC / Cells / Pos Mode")

MissingPerSamplePlot(vf.med.neg.raw, "hVPA_FAnM") +
  ggtitle("Missing Per Sample\nVPA + FA HILIC / Media / Neg Mode")

MissingPerSamplePlot(vf.med.pos.raw, "hVPA_FApM") +
  ggtitle("Missing Per Sample\nVPA + FA HILIC / Media / Pos Mode")

Q: Are any of the compounds more than 20% missing in the experimental sample group? If there are any, they will be excluded from analysis.

(vf.cell.neg.cmpnd.to.excl <- MissingPerCompound(vf.cell.neg.raw, "hVPA_FAnC") %>% 
  filter(percent_na > 20))
# A tibble: 5 x 4
  Compound     na_count n_samples percent_na
  <chr>           <int>     <int>      <dbl>
1 hVPA_FAnC139       14        22       63.6
2 hVPA_FAnC72         8        22       36.4
3 hVPA_FAnC113        7        22       31.8
4 hVPA_FAnC138        5        22       22.7
5 hVPA_FAnC64         5        22       22.7
MissingPerCompound(vf.cell.pos.raw, "hVPA_FApC") %>% 
  filter(percent_na > 20)
# A tibble: 0 x 4
# ... with 4 variables: Compound <chr>, na_count <int>, n_samples <int>,
#   percent_na <dbl>
MissingPerCompound(vf.med.neg.raw, "hVPA_FAnM") %>% 
  filter(percent_na > 20)
# A tibble: 0 x 4
# ... with 4 variables: Compound <chr>, na_count <int>, n_samples <int>,
#   percent_na <dbl>
MissingPerCompound(vf.med.pos.raw, "hVPA_FApM") %>% 
  filter(percent_na > 20)
# A tibble: 0 x 4
# ... with 4 variables: Compound <chr>, na_count <int>, n_samples <int>,
#   percent_na <dbl>

2.3 Negative Controls and Compound Elimination

2.3.1 Cells / Negative Mode

vf.cell.neg.raw.grp.mean <- vf.cell.neg.raw %>% 
  group_by(Group) %>% 
  summarize_at(vars(matches("hVPA_FAnC")), mean, na.rm = TRUE) %>% 
  gather(key = "Compound", value = "Grp_mean_abun", -Group)
vf.cell.neg.raw.grp.mean %>% 
  ggplot(aes(log2(Grp_mean_abun), color = Group)) +
  geom_density(size = 2, alpha = 0.8) +
  ggtitle("Distribution of compound means\nVPA + FA HILIC / Cells / Negative Mode\nGrouped by sample type")

vf.cell.neg.raw.grp.mean.order <- vf.cell.neg.raw.grp.mean %>% 
  filter(Group == "sample") %>% 
  arrange(Grp_mean_abun)
vf.cell.neg.raw %>% 
  select(Samples, Group, starts_with("hVPA_FAnC")) %>% 
  gather("Compound", value = "Abundance", -c(Samples, Group)) %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.neg.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Abundance), color = Group, group = Samples)) + 
  geom_line(alpha = 0.1, size = 1) + 
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  geom_line(
    data = vf.cell.neg.raw.grp.mean %>% 
      mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.neg.raw.grp.mean.order$Compound)), 
    aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group),
    size = 0.5
    ) +
  ggtitle("Profile Plot of all compound abundances\nWith average per sample type overlaid\nVPA + FA HILIC/ Cells / Negative Mode")

vf.cell.neg.raw.grp.mean %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.neg.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group)) +
  geom_point(size = 1, alpha = 0.8) +
  geom_line(alpha = 0.8) +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  ylab("log2(Sample Type Mean)") +
  ggtitle("Profile Plot of compound means by sample type only\nVPA + FA HILIC / Cells / Negative Mode")

vf.cell.neg.raw.grp.diff <- vf.cell.neg.raw.grp.mean %>% 
  spread(Group, Grp_mean_abun) %>% 
  mutate(
    smpl_solv_diff = sample / solv,
    )
vf.cell.neg.raw.grp.diff %>% 
  ggplot(aes(log2(smpl_solv_diff))) +
  geom_histogram(bins = 50)

vf.cell.neg.cmpnd.to.incl <- vf.cell.neg.raw.grp.diff %>% 
  filter(smpl_solv_diff > 2.5 | is.na(smpl_solv_diff)) %>% 
  filter(!(Compound %in% vf.cell.neg.cmpnd.to.excl$Compound))
# original number of metabolites
nrow(vf.cell.neg.raw.grp.diff)
[1] 139
# number of metabolites after filtering 
nrow(vf.cell.neg.cmpnd.to.incl)
[1] 132

2.3.2 Cells / Positive Mode

vf.cell.pos.raw.grp.mean <- vf.cell.pos.raw %>% 
  group_by(Group) %>% 
  summarize_at(vars(matches("hVPA_FApC")), mean, na.rm = TRUE) %>% 
  gather(key = "Compound", value = "Grp_mean_abun", -Group)
vf.cell.pos.raw.grp.mean %>% 
  ggplot(aes(log2(Grp_mean_abun), color = Group)) +
  geom_density(size = 2, alpha = 0.8) +
  ggtitle("Distribution of compound means\nVPA + FA HILIC / Cells / Positive Mode\nGrouped by sample type")

vf.cell.pos.raw.grp.mean.order <- vf.cell.pos.raw.grp.mean %>% 
  filter(Group == "sample") %>% 
  arrange(Grp_mean_abun)
vf.cell.pos.raw %>% 
  select(Samples, Group, starts_with("hVPA_FApC")) %>% 
  gather("Compound", value = "Abundance", -c(Samples, Group)) %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.pos.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Abundance), color = Group, group = Samples)) + 
  geom_line(alpha = 0.1, size = 1) + 
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  geom_line(
    data = vf.cell.pos.raw.grp.mean %>% 
      mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.pos.raw.grp.mean.order$Compound)), 
    aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group),
    size = 0.5
    ) +
  ggtitle("Profile Plot of all compound abundances\nWith average per sample type overlaid\nVPA + FA HILIC / Cells / Positive Mode")

vf.cell.pos.raw.grp.mean %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.cell.pos.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group)) +
  geom_point(size = 1, alpha = 0.8) +
  geom_line(alpha = 0.8) +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  ylab("log2(Sample Type Mean)") +
  ggtitle("Profile Plot of compound means by sample type only\nVPA + FA HILIC / Cells / Positive Mode")

vf.cell.pos.raw.grp.diff <- vf.cell.pos.raw.grp.mean %>% 
  spread(Group, Grp_mean_abun) %>% 
  mutate(
    smpl_solv_diff = sample / solv
    )
vf.cell.pos.raw.grp.diff %>% 
  ggplot(aes(log2(smpl_solv_diff))) +
  geom_histogram(bins = 50)

# include compounds with FC > 2.5 or FC is NA (indication of NA in solv)
vf.cell.pos.cmpnd.to.incl <- vf.cell.pos.raw.grp.diff %>% 
  filter(smpl_solv_diff > 2.5 | is.na(smpl_solv_diff))
# original number of metabolites
nrow(vf.cell.pos.raw.grp.diff)
[1] 144
# filtered number
nrow(vf.cell.pos.cmpnd.to.incl)
[1] 142

2.3.3 Media / Negative Mode

vf.med.neg.raw.grp.mean <- vf.med.neg.raw %>% 
  group_by(Group) %>% 
  summarize_at(vars(matches("hVPA_FAnM")), mean, na.rm = TRUE) %>% 
  gather(key = "Compound", value = "Grp_mean_abun", -Group)
vf.med.neg.raw.grp.mean %>% 
  ggplot(aes(log2(Grp_mean_abun), color = Group)) +
  geom_density(size = 2, alpha = 0.8) +
  ggtitle("Distribution of compound means\nVPA + FA / Media HILIC / Negative Mode\nGrouped by sample type")

vf.med.neg.raw.grp.mean.order <- vf.med.neg.raw.grp.mean %>% 
  filter(Group == "sample") %>% 
  arrange(Grp_mean_abun)
vf.med.neg.raw %>% 
  select(Samples, Group, starts_with("hVPA_FAnM")) %>% 
  gather("Compound", value = "Abundance", -c(Samples, Group)) %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.med.neg.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Abundance), color = Group, group = Samples)) + 
  geom_line(alpha = 0.1, size = 2) + 
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  geom_line(
    data = vf.med.neg.raw.grp.mean %>% 
      mutate(Cmpnd_sort = factor(Compound, levels = vf.med.neg.raw.grp.mean.order$Compound)), 
    aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group),
    size = 0.5
    ) +
  ggtitle("Profile Plot of all compound abundances\nWith average per sample type overlaid\nVPA + FA HILIC / Media / Negative Mode")

vf.med.neg.raw.grp.mean %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.med.neg.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group)) +
  geom_point(size = 1, alpha = 0.8) +
  geom_line(alpha = 0.8) +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  ylab("log2(Sample Type Mean)") +
  ggtitle("Profile Plot of compound means by sample type only\nVPA + FA HILIC / Media / Negative Mode")

vf.med.neg.raw.grp.diff <- vf.med.neg.raw.grp.mean %>% 
  spread(Group, Grp_mean_abun) %>% 
  mutate(
    smpl_solv_diff = sample / solv
    )
vf.med.neg.raw.grp.diff %>% 
  ggplot(aes(log2(smpl_solv_diff))) +
  geom_histogram(bins = 25)

# include compounds with FC > 2.5 or FC is NA (indication of NA in solv)
vf.med.neg.cmpnd.to.incl <- vf.med.neg.raw.grp.diff %>% 
  filter(smpl_solv_diff > 2.5 | is.na(smpl_solv_diff))
nrow(vf.med.neg.raw.grp.diff)
[1] 56
nrow(vf.med.neg.cmpnd.to.incl)
[1] 52

2.3.4 Media / Negative Mode

vf.med.pos.raw.grp.mean <- vf.med.pos.raw %>% 
  group_by(Group) %>% 
  summarize_at(vars(matches("hVPA_FApM")), mean, na.rm = TRUE) %>% 
  gather(key = "Compound", value = "Grp_mean_abun", -Group)
vf.med.pos.raw.grp.mean %>% 
  ggplot(aes(log2(Grp_mean_abun), color = Group)) +
  geom_density(size = 2, alpha = 0.8) +
  ggtitle("Distribution of compound means\nVPA + FA HILIC / Media / Positive Mode\nGrouped by sample type")

vf.med.pos.raw.grp.mean.order <- vf.med.pos.raw.grp.mean %>% 
  filter(Group == "sample") %>% 
  arrange(Grp_mean_abun)
vf.med.pos.raw %>% 
  select(Samples, Group, starts_with("hVPA_FApM")) %>% 
  gather("Compound", value = "Abundance", -c(Samples, Group)) %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.med.pos.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Abundance), color = Group, group = Samples)) + 
  geom_line(alpha = 0.1, size = 2) + 
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  geom_line(
    data = vf.med.pos.raw.grp.mean %>% 
      mutate(Cmpnd_sort = factor(Compound, levels = vf.med.pos.raw.grp.mean.order$Compound)), 
    aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group),
    size = 0.5
    ) +
  ggtitle("Profile Plot of all compound abundances\nWith average per sample type overlaid\nVPA + FA HILIC / Media / Positive Mode")

vf.med.pos.raw.grp.mean %>% 
  mutate(Cmpnd_sort = factor(Compound, levels = vf.med.pos.raw.grp.mean.order$Compound)) %>% 
  ggplot(aes(Cmpnd_sort, log2(Grp_mean_abun), color = Group, group = Group)) +
  geom_point(size = 1, alpha = 0.8) +
  geom_line(alpha = 0.8) +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
  xlab("Compound") +
  ylab("log2(Sample Type Mean)") +
  ggtitle("Profile Plot of compound means by sample type only\nVPA + FA HILIC / Media / Positive Mode")

vf.med.pos.raw.grp.diff <- vf.med.pos.raw.grp.mean %>% 
  spread(Group, Grp_mean_abun) %>% 
  mutate(
    smpl_solv_diff = sample / solv
    )
vf.med.pos.raw.grp.diff %>% 
  ggplot(aes(log2(smpl_solv_diff))) +
  geom_histogram(bins = 25)

# include compounds with FC > 2.5 or FC is NA (indication of NA in solv)
vf.med.pos.cmpnd.to.incl <- vf.med.pos.raw.grp.diff %>% 
  filter(smpl_solv_diff > 2.5 | is.na(smpl_solv_diff)) 
nrow(vf.med.pos.raw.grp.diff)
[1] 47
nrow(vf.med.pos.cmpnd.to.incl)
[1] 47

3 Data Prep and Preliminary Analysis

3.1 Cleanup

vf.cell.neg.noNA <- vf.cell.neg.raw %>% 
  select(Samples:Plate, one_of(vf.cell.neg.cmpnd.to.incl$Compound)) %>% 
  ReplaceNAwMinLogTransformMult("hVPA_FAnC")
vf.cell.pos.noNA <- vf.cell.pos.raw %>% 
  select(Samples:Plate, one_of(vf.cell.pos.cmpnd.to.incl$Compound)) %>% 
  ReplaceNAwMinLogTransformMult("hVPA_FApC")
vf.med.neg.noNA <- vf.med.neg.raw %>% 
  select(Samples:Plate, one_of(vf.med.neg.cmpnd.to.incl$Compound)) %>% 
  ReplaceNAwMinLogTransformMult("hVPA_FAnM")
vf.med.pos.noNA <- vf.med.pos.raw %>% 
  select(Samples:Plate, one_of(vf.med.pos.cmpnd.to.incl$Compound)) %>%  
  ReplaceNAwMinLogTransformMult("hVPA_FApM")

3.2 Distribution plots

3.2.1 Cells / Negative Mode

vf.cell.neg.noNA.gathered <- vf.cell.neg.noNA %>% 
  gather(
    key = "Metabolite", "Abundance", 
    which(colnames(vf.cell.neg.noNA) == "hVPA_FAnC10"):ncol(vf.cell.neg.noNA)
    )
# plot all abundances in a sample, grouped by sample as a boxplot
vf.cell.neg.noNA.gathered %>% 
  ggplot(aes(Samples, Abundance, fill = Group)) +
  geom_boxplot() +
  geom_boxplot(aes(color = Group), fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0, show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
  ylab("log2(Abundance)") +
  ggtitle("Boxplot of compound abundances\nAll samples\nVPA + FA HILIC / Cells / Negative Mode")

# same data format, but as ridge plots
vf.cell.neg.noNA.gathered %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Group)) + 
  geom_density_ridges(scale = 15) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nAll samples\nVPA + FA HILIC/ Cells / Negative Mode")
Picking joint bandwidth of 0.972

# experimental samples only
vf.cell.neg.noNA.gathered %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Treatment)) + 
  geom_density_ridges(scale = 10) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Cells / Negative Mode")
Picking joint bandwidth of 0.93

# overlay the distributions for another look
vf.cell.neg.noNA.gathered %>%
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(Abundance, group = Samples, color = Treatment)) +
  geom_density(alpha = 0.8, size = 0.75) +
  xlab("log2(Abundance)") +
  ggtitle("Density plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Cells / Negative Mode")

3.2.2 Cells / Positive Mode

vf.cell.pos.noNA.gathered <- vf.cell.pos.noNA %>% 
  gather(
    key = "Metabolite", "Abundance", 
    which(colnames(vf.cell.pos.noNA) == "hVPA_FApC1"):ncol(vf.cell.pos.noNA)
    )
vf.cell.pos.noNA.gathered %>% 
  ggplot(aes(Samples, Abundance, fill = Group)) +
  geom_boxplot() +
  geom_boxplot(aes(color = Group), fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0, show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
  ylab("log2(Abundance)") +
  ggtitle("Boxplot of compound abundances\nAll samples\nVPA + FA HILIC / Cells / Positive Mode")

vf.cell.pos.noNA.gathered %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Group)) + 
  geom_density_ridges(scale = 15) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nAll samples\nVPA + FA HILIC / Cells / Positive Mode")
Picking joint bandwidth of 1.19

vf.cell.pos.noNA.gathered %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Treatment)) + 
  geom_density_ridges(scale = 10) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Cells / Positive Mode")
Picking joint bandwidth of 1.18

vf.cell.pos.noNA.gathered %>%
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(Abundance, group = Samples, color = Treatment)) +
  geom_density(alpha = 0.8, size = 0.75) +
  xlab("log2(Abundance)") +
  ggtitle("Density plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Cells / Positive Mode")

3.2.3 Media / Negative Mode

vf.med.neg.noNA.gathered <- vf.med.neg.noNA %>% 
  gather(
    key = "Metabolite", "Abundance", 
    which(colnames(vf.med.neg.noNA) == "hVPA_FAnM10"):ncol(vf.med.neg.noNA)
    )
vf.med.neg.noNA.gathered %>% 
  ggplot(aes(Samples, Abundance, fill = Group)) +
  geom_boxplot() +
  geom_boxplot(aes(color = Group), fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0, show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
  ylab("log2(Abundance)") +
  ggtitle("Boxplot of compound abundances\nAll samples\nVPA + FA HILIC / Media / Negative Mode")

vf.med.neg.noNA.gathered %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Group)) + 
  geom_density_ridges(scale = 15) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nAll samples\nVPA + FA HILIC / Media / Negative Mode")
Picking joint bandwidth of 0.885

vf.med.neg.noNA.gathered %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Treatment)) + 
  geom_density_ridges(scale = 10) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Media / Negative Mode")
Picking joint bandwidth of 0.883

vf.med.neg.noNA.gathered %>%
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(Abundance, group = Samples, color = Treatment)) +
  geom_density(alpha = 0.8, size = 0.75) +
  xlab("log2(Abundance)") +
  ggtitle("Density plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Media / Negative Mode")

3.2.4 Media / Positive Mode

vf.med.pos.noNA.gathered <- vf.med.pos.noNA %>% 
  gather(
    key = "Metabolite", "Abundance", 
    which(colnames(vf.med.pos.noNA) == "hVPA_FApM1"):ncol(vf.med.pos.noNA)
    )
vf.med.pos.noNA.gathered %>% 
  ggplot(aes(Samples, Abundance, fill = Group)) +
  geom_boxplot() +
  geom_boxplot(aes(color = Group), fatten = NULL, fill = NA, coef = 0, outlier.alpha = 0, show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
  ylab("log2(Abundance)") +
  ggtitle("Boxplot of compound abundances\nAll samples\nVPA + FA HILIC / Media / Positive Mode")

vf.med.pos.noNA.gathered %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Group)) + 
  geom_density_ridges(scale = 15) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nAll samples\nVPA + FA HILIC / Media / Positive Mode")
Picking joint bandwidth of 1.18

vf.med.pos.noNA.gathered %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(y = Samples, x = Abundance, fill = Treatment)) + 
  geom_density_ridges(scale = 10) +
  theme_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +
  ggtitle("Ridge plot of compound abundances\nExperimental samples only\nVPA + FA HILIC  / Media / Positive Mode")
Picking joint bandwidth of 1.18

vf.med.pos.noNA.gathered %>%
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(Abundance, group = Samples, color = Treatment)) +
  geom_density(alpha = 0.8, size = 0.75) +
  xlab("log2(Abundance)") +
  ggtitle("Density plot of compound abundances\nExperimental samples only\nVPA + FA HILIC / Media / Positive Mode")

3.3 Principal Component Analysis

Some plots to understand the relationship between the samples, QC samples, solvent, and empty samples in some cases.

3.3.1 Cells / Negative Mode

### PCA on all Samples ###
vf.cell.neg.full.pca <- vf.cell.neg.noNA %>% 
  select(starts_with("hVPA_FAnC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.neg.full.pca$sdev ^ 2) * 100 / sum(vf.cell.neg.full.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nAll samples only\nVPA + FA HILIC / Cells / Negative Mode",
  type = "b"
  )

vf.cell.neg.full.pca.x <- as.data.frame(vf.cell.neg.full.pca$x)
row.names(vf.cell.neg.full.pca.x) <- vf.cell.neg.noNA$Samples
vf.cell.neg.full.pca.x <- vf.cell.neg.full.pca.x %>% 
  bind_cols(vf.cell.neg.noNA %>% select(Group:Plate))
vf.cell.neg.full.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = Group)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (96.4% Var)") +
  ylab("PC2 (14.5% Var)") +
  ggtitle("Principal Component Analysis\nAll Samples\nVPA + FA HILIC / Cells / Negative Mode")

### Samples and mix ###
vf.cell.neg.smpl.mix.pca <- vf.cell.neg.noNA %>% 
  filter(Group == "sample" | Group == "mix") %>% 
  select(starts_with("hVPA_FAnC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.neg.smpl.mix.pca$sdev ^ 2) * 100 / sum(vf.cell.neg.smpl.mix.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nSamples and Mix\nVPA + FA HILIC / Cells / Negative Mode",
  type = "b"
  )

vf.cell.neg.smpl.mix.pca.x <- as.data.frame(vf.cell.neg.smpl.mix.pca$x)
vf.cell.neg.smpl.mix.pca.x <- vf.cell.neg.smpl.mix.pca.x %>% 
  bind_cols(
    vf.cell.neg.noNA %>% 
      filter(Group == "sample" | Group == "mix") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.cell.neg.smpl.mix.pca.x) <- vf.cell.neg.smpl.mix.pca.x$Samples
vf.cell.neg.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC1, y = PC2, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (63.1% Var)") +
  ylab("PC2 (14.0% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA HILIC / Cells / Negative Mode")

vf.cell.neg.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC3, y = PC4, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (6.2% Var)") +
  ylab("PC4 (4.7% Var)") +
  ggtitle("Principal Component Analysis\nSamples and mix\nVPA + FA HILIC / Cells / Negative Mode")

### Experimental Samples Only ###
vf.cell.neg.smpl.pca <- vf.cell.neg.noNA %>% 
  filter(Group == "sample") %>% 
  select(starts_with("hVPA_FAnC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.neg.smpl.pca$sdev ^ 2) * 100 / sum(vf.cell.neg.smpl.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nExperimental samples only\nVPA + FA HILIC / Cells / Negative Mode",
  type = "b"
  )

vf.cell.neg.smpl.pca.x <- as.data.frame(vf.cell.neg.smpl.pca$x)
vf.cell.neg.smpl.pca.x <- vf.cell.neg.smpl.pca.x %>% 
  bind_cols(
    vf.cell.neg.noNA %>% 
      filter(Group == "sample") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.cell.neg.smpl.pca.x) <- vf.cell.neg.smpl.pca.x$Samples
vf.cell.neg.smpl.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (65.3% Var)") +
  ylab("PC2 (14.6% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA HILIC / Cells / Negative Mode")

vf.cell.neg.smpl.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (5.7% Var)") +
  ylab("PC4 (4.2% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA HILIC/ Cells / Negative Mode")

3.3.2 Cells / Positive Mode

vf.cell.pos.full.pca <- vf.cell.pos.noNA %>% 
  select(starts_with("hVPA_FApC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.pos.full.pca$sdev ^ 2) * 100 / sum(vf.cell.pos.full.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nAll samples only\nVPA + FA HILIC / Cells / Positive Mode",
  type = "b"
  )

vf.cell.pos.full.pca.x <- as.data.frame(vf.cell.pos.full.pca$x)
row.names(vf.cell.pos.full.pca.x) <- vf.cell.pos.noNA$Samples
vf.cell.pos.full.pca.x <- vf.cell.pos.full.pca.x %>% 
  bind_cols(vf.cell.pos.noNA %>% select(Group:Plate))
vf.cell.pos.full.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = Group)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (92.4% Var)") +
  ylab("PC2 (2.7% Var)") +
  ggtitle("Principal Component Analysis\nAll Samples\nVPA + FA HILIC / Cells / Positive Mode")

### Samples and Mix ###
vf.cell.pos.smpl.mix.pca <- vf.cell.pos.noNA %>% 
  filter(Group == "sample" | Group == "mix") %>% 
  select(starts_with("hVPA_FApC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.pos.smpl.mix.pca$sdev ^ 2) * 100 / sum(vf.cell.pos.smpl.mix.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nSamples and Mix\nVPA + FA HILIC / Cells / Positive Mode",
  type = "b"
  )

vf.cell.pos.smpl.mix.pca.x <- as.data.frame(vf.cell.pos.smpl.mix.pca$x)
vf.cell.pos.smpl.mix.pca.x <- vf.cell.pos.smpl.mix.pca.x %>% 
  bind_cols(
    vf.cell.pos.noNA %>% 
      filter(Group == "sample" | Group == "mix") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.cell.pos.smpl.mix.pca.x) <- vf.cell.pos.smpl.mix.pca.x$Samples
vf.cell.pos.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_", remove = FALSE) %>% 
  ggplot(aes(x = PC1, y = PC2, color = Treatment, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (62.9% Var)") +
  ylab("PC2 (13.6% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA / Cells / Positive Mode")

vf.cell.pos.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC3, y = PC4, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (7.2% Var)") +
  ylab("PC4 (3.8% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA HILIC / Cells / Positive Mode")

### Experimental Samples Only ###
vf.cell.pos.smpl.pca <- vf.cell.pos.noNA %>% 
  filter(Group == "sample") %>% 
  select(starts_with("hVPA_FApC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.cell.pos.smpl.pca$sdev ^ 2) * 100 / sum(vf.cell.pos.smpl.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nExperimental samples only\nVPA + FA HILIC/ Cells / Positive Mode",
  type = "b"
  )

vf.cell.pos.smpl.pca.x <- as.data.frame(vf.cell.pos.smpl.pca$x)
vf.cell.pos.smpl.pca.x <- vf.cell.pos.smpl.pca.x %>% 
  bind_cols(
    vf.cell.pos.noNA %>% 
      filter(Group == "sample") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.cell.pos.smpl.pca.x) <- vf.cell.pos.smpl.pca.x$Samples
vf.cell.pos.smpl.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (61.2% Var)") +
  ylab("PC2 (15.2% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA HILIC / Cells / Positive Mode")

vf.cell.pos.smpl.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (7.7% Var)") +
  ylab("PC4 (4.3% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA HILIC / Cells / Positive Mode")

3.3.3 Media / Negative Mode

### PCA on all Samples ###
vf.med.neg.full.pca <- vf.med.neg.noNA %>% 
  select(starts_with("hVPA_FAnM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.neg.full.pca$sdev ^ 2) * 100 / sum(vf.med.neg.full.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nAll samples only\nVPA + FA / Media / Negative Mode",
  type = "b"
  )

vf.med.neg.full.pca.x <- as.data.frame(vf.med.neg.full.pca$x)
row.names(vf.med.neg.full.pca.x) <- vf.med.neg.noNA$Samples
vf.med.neg.full.pca.x <- vf.med.neg.full.pca.x %>% 
  bind_cols(vf.med.neg.noNA %>% select(Group:Plate))
vf.med.neg.full.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = Group)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (89.8% Var)") +
  ylab("PC2 (4.7% Var)") +
  ggtitle("Principal Component Analysis\nAll Samples\nVPA + FA / Media / Negative Mode")

### Samples and Mix ###
vf.med.neg.smpl.mix.pca <- vf.med.neg.noNA %>% 
  filter(Group == "sample" | Group == "mix") %>% 
  select(starts_with("hVPA_FAnM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.neg.smpl.mix.pca$sdev ^ 2) * 100 / sum(vf.med.neg.smpl.mix.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nSamples and Mix\nVPA + FA / Media / Negative Mode",
  type = "b"
  )

vf.med.neg.smpl.mix.pca.x <- as.data.frame(vf.med.neg.smpl.mix.pca$x)
vf.med.neg.smpl.mix.pca.x <- vf.med.neg.smpl.mix.pca.x %>% 
  bind_cols(
    vf.med.neg.noNA %>% 
      filter(Group == "sample" | Group == "mix") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.med.neg.smpl.mix.pca.x) <- vf.med.neg.smpl.mix.pca.x$Samples
vf.med.neg.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC1, y = PC2, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (67.1% Var)") +
  ylab("PC2 (19.6% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA / Media / Negative Mode")

vf.med.neg.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC3, y = PC4, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (4.3% Var)") +
  ylab("PC4 (2.1% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA / Media / Negative Mode")

### Experimental Samples Only ###
vf.med.neg.smpl.pca <- vf.med.neg.noNA %>% 
  filter(Group == "sample") %>% 
  select(starts_with("hVPA_FAnM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.neg.smpl.pca$sdev ^ 2) * 100 / sum(vf.med.neg.smpl.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nExperimental samples only\nVPA + FA / Media / Negative Mode",
  type = "b"
  )

vf.med.neg.smpl.pca.x <- as.data.frame(vf.med.neg.smpl.pca$x)
vf.med.neg.smpl.pca.x <- vf.med.neg.smpl.pca.x %>% 
  bind_cols(
    vf.med.neg.noNA %>% 
      filter(Group == "sample") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.med.neg.smpl.pca.x) <- vf.med.neg.smpl.pca.x$Samples
vf.med.neg.smpl.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (74.4% Var)") +
  ylab("PC2 (12.1% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA / Media / Negative Mode")

vf.med.neg.smpl.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (4.5% Var)") +
  ylab("PC4 (2.2% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA / Media / Negative Mode")

3.3.4 Media / Positive Mode

### PCA on all Samples ###
vf.med.pos.full.pca <- vf.med.pos.noNA %>% 
  select(starts_with("hVPA_FApM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.pos.full.pca$sdev ^ 2) * 100 / sum(vf.med.pos.full.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nAll samples only\nVPA + FA / Media / Positive Mode",
  type = "b"
  )

vf.med.pos.full.pca.x <- as.data.frame(vf.med.pos.full.pca$x)
row.names(vf.med.pos.full.pca.x) <- vf.med.pos.noNA$Samples
vf.med.pos.full.pca.x <- vf.med.pos.full.pca.x %>% 
  bind_cols(vf.med.pos.noNA %>% select(Group:Plate))
vf.med.pos.full.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = Group)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (88.6% Var)") +
  ylab("PC2 (4.7% Var)") +
  ggtitle("Principal Component Analysis\nAll Samples\nVPA + FA / Media / Positive Mode")

### Samples and mix ###
vf.med.pos.smpl.mix.pca <- vf.med.pos.noNA %>% 
  filter(Group == "sample" | Group == "mix") %>% 
  select(starts_with("hVPA_FApM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.pos.smpl.mix.pca$sdev ^ 2) * 100 / sum(vf.med.pos.smpl.mix.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nSamples and Mix\nVPA + FA / Media / Positive Mode",
  type = "b"
  )

vf.med.pos.smpl.mix.pca.x <- as.data.frame(vf.med.pos.smpl.mix.pca$x)
vf.med.pos.smpl.mix.pca.x <- vf.med.pos.smpl.mix.pca.x %>% 
  bind_cols(
    vf.med.pos.noNA %>% 
      filter(Group == "sample" | Group == "mix") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.med.pos.smpl.mix.pca.x) <- vf.med.pos.smpl.mix.pca.x$Samples
vf.med.pos.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC1, y = PC2, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (53.7% Var)") +
  ylab("PC2 (18.9% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA / Media / Positive Mode")

vf.med.pos.smpl.mix.pca.x %>% 
  unite("Treatment", VPA:FA, sep = "_") %>% 
  ggplot(aes(x = PC3, y = PC4, color = Treatment)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (8.7% Var)") +
  ylab("PC4 (6.0% Var)") +
  ggtitle("Principal Component Analysis\nSamples and Mix\nVPA + FA / Media / Positive Mode")

### Experimental Samples Only ###
vf.med.pos.smpl.pca <- vf.med.pos.noNA %>% 
  filter(Group == "sample") %>% 
  select(starts_with("hVPA_FApM")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
plot(
  (vf.med.pos.smpl.pca$sdev ^ 2) * 100 / sum(vf.med.pos.smpl.pca$sdev ^ 2), 
  xlab = "Principal Component",
  ylab = "Variance Explained",
  main = "Percent variance explained by each principal component\nExperimental samples only\nVPA + FA / Media / Positive Mode",
  type = "b"
  )

vf.med.pos.smpl.pca.x <- as.data.frame(vf.med.pos.smpl.pca$x)
vf.med.pos.smpl.pca.x <- vf.med.pos.smpl.pca.x %>% 
  bind_cols(
    vf.med.pos.noNA %>% 
      filter(Group == "sample") %>% 
      select(Samples, Group:Plate)
  )
row.names(vf.med.pos.smpl.pca.x) <- vf.med.pos.smpl.pca.x$Samples
vf.med.pos.smpl.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (53.8% Var)") +
  ylab("PC2 (19.8% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA / Media / Positive Mode")

vf.med.pos.smpl.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = FA, shape = VPA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (8.8% Var)") +
  ylab("PC4 (5.8% Var)") +
  ggtitle("Principal Component Analysis\nExperimental samples only\nVPA + FA / Media / Positive Mode")

4 Batch Effects and Signifiance Testing

4.1 Cells / Negative Mode

# sample prep
vf.cell.neg.smpl.data <- vf.cell.neg.noNA %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_")
vf.cell.neg.data.for.sva <- as.matrix(
  vf.cell.neg.smpl.data[, which(
    colnames(vf.cell.neg.smpl.data) == "hVPA_FAnC10"
    ):ncol(vf.cell.neg.smpl.data)]
  )
row.names(vf.cell.neg.data.for.sva) <- vf.cell.neg.smpl.data$Samples
vf.cell.neg.data.for.sva <- t(vf.cell.neg.data.for.sva)
# pheno prep
vf.cell.neg.data.pheno <- as.data.frame(vf.cell.neg.smpl.data[, 5:6])
row.names(vf.cell.neg.data.pheno) <- vf.cell.neg.smpl.data$Samples
# sva calculation
vf.cell.neg.mod.vf <- model.matrix(~ 0 + as.factor(Treatment), data = vf.cell.neg.data.pheno)
vf.cell.neg.mod0 <- model.matrix(~ 1, data = vf.cell.neg.data.pheno)
set.seed(2018)
num.sv(vf.cell.neg.data.for.sva, vf.cell.neg.mod.vf, method = "be")
[1] 1
set.seed(2018)
num.sv(vf.cell.neg.data.for.sva, vf.cell.neg.mod.vf, method = "leek")
[1] 1
set.seed(2018)
vf.cell.neg.sv <- sva(vf.cell.neg.data.for.sva, vf.cell.neg.mod.vf, vf.cell.neg.mod0)
Number of significant surrogate variables is:  1 
Iteration (out of 5 ):1  2  3  4  5  
# extract the surrogate variables
vf.cell.neg.surr.var <- as.data.frame(vf.cell.neg.sv$sv)
colnames(vf.cell.neg.surr.var) <- c("S1")
colnames(vf.cell.neg.mod.vf) <- c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
# combine the full model matrix and the surrogate variables into one
vf.cell.neg.design.sv <- cbind(vf.cell.neg.mod.vf, vf.cell.neg.surr.var)
vf.cell.neg.cont.mat <- makeContrasts(
  vpa_lowFA = vpa_only - cntrl, 
  vpa_highFA = fa_and_vpa - fa_only,
  FA_diff = (fa_and_vpa - fa_only) - (vpa_only - cntrl),
  levels = c("cntrl", "fa_only", "vpa_only", "fa_and_vpa", "S1")
  )
# fit the model/design matrix
vf.cell.neg.eb <- vf.cell.neg.data.for.sva %>% 
  lmFit(vf.cell.neg.design.sv) %>% 
  contrasts.fit(vf.cell.neg.cont.mat) %>% 
  eBayes()
# pull out the results for each metabolite for each comparison
vf.cell.neg.eb.tidy <- tidy(vf.cell.neg.eb) %>% 
  mutate(
    adj_pval = p.adjust(p.value, method = "bonferroni"),
    FC = 2 ^ estimate,
    change_in_vpa = ifelse(FC < 1, "down", "up")
    ) %>% 
  rename(compound_short = gene)
# volcano plot
vf.cell.neg.eb.tidy %>% 
  ggplot(aes(estimate, -log10(adj_pval))) +
  geom_point(size = 2, alpha = 0.5) +
  geom_hline(linetype = "dashed", color = "#009E73", yintercept = -log10(0.05)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1.2)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1/1.2)) +
  xlim(-2.5, 2.5)  +
  xlab("log2(FC)") +
  ylab("-log10(adjusted p-value)") +
  ggtitle("Volcano plot\nVPA + FA HILIC / Cells / Negative Mode")

# select statistically significant hits with a certain FC:
vf.cell.neg.hits <- vf.cell.neg.eb.tidy %>% 
  filter(adj_pval < 0.05 & (FC > 1.2 | FC < 1/1.2)) %>% 
  inner_join(vf.cell.neg.compound.info, by = "compound_short")
# how many metabolites are significant across the different contrats
table(vf.cell.neg.hits$term)

   FA_diff vpa_highFA  vpa_lowFA 
         0         13         14 
# significant metabolites
sort(unique(vf.cell.neg.hits$compound_full))
 [1] "3-Sulfinoalanine"                      
 [2] "3,4-Dihydroxyphenylacetic Acid (DOPAC)"
 [3] "ATP"                                   
 [4] "BAIBA"                                 
 [5] "Caprylic Acid"                         
 [6] "Creatinine"                            
 [7] "Cystathionine"                         
 [8] "D-Galactitol"                          
 [9] "D-Glucose 6-phosphate"                 
[10] "D-Sorbitol"                            
[11] "Docosahexaenoic Acid (22:6 n-3)"       
[12] "GABA"                                  
[13] "Glutamic Acid"                         
[14] "GSSG"                                  
[15] "myo-Inositol"                          
[16] "N-Acetylglutamic Acid"                 
[17] "N-Acetylserine"                        
[18] "Oleic Acid"                            
[19] "Palmitoleic Acid"                      
[20] "Proline"                               
[21] "Tryptophan"                            
[22] "UDP-N-Acetylgalactosamine"             
[23] "UTP"                                   
vf.cell.neg.hits.tally2 <- vf.cell.neg.hits %>% 
  group_by(compound_short, compound_full) %>% 
  count() %>% 
  filter(n == 2)
vf.cell.neg.lowFA.hits <- vf.cell.neg.hits %>% 
  filter(term == "vpa_lowFA" & !(compound_short %in% vf.cell.neg.hits.tally2$compound_short))
vf.cell.neg.highFA.hits <- vf.cell.neg.hits %>% 
  filter(term == "vpa_highFA" & !(compound_short %in% vf.cell.neg.hits.tally2$compound_short))
vf.cell.neg.both.hits <- vf.cell.neg.hits %>% 
  filter(compound_short %in% vf.cell.neg.hits.tally2$compound_short) %>% 
  arrange(compound_short, term)
vf.cell.neg.both.hits %>% 
  select(compound_full, term, FC) %>% 
  spread(key = "term", value = "FC") %>% 
  ggplot(aes(vpa_lowFA, vpa_highFA)) +
  geom_point(size = 2, alpha = 0.8) +
  geom_abline(intercept = 0, slope = 1, color = "blue", alpha = 0.8)

vf.cell.neg.both.hits %>% 
  select(compound_full, term, FC) %>% 
  spread(key = "term", value = "FC") %>% 
  mutate(diff = vpa_highFA - vpa_lowFA) %>% 
  arrange(diff)
# A tibble: 4 x 4
  compound_full                   vpa_highFA vpa_lowFA    diff
  <chr>                                <dbl>     <dbl>   <dbl>
1 Docosahexaenoic Acid (22:6 n-3)      1.86      2.94  -1.08  
2 D-Sorbitol                           0.456     0.374  0.0824
3 Cystathionine                        2.25      2.00   0.253 
4 Proline                              3.00      2.66   0.338 
### Plotting ###
vf.cell.neg.gathered <- vf.cell.neg.noNA %>%
  filter(Group == "sample") %>% 
  bind_cols(vf.cell.neg.surr.var) %>% 
  select(Samples, VPA, FA, S1, starts_with("hVPA_FAnC")) %>% 
  gather(key = "Compound", value = "Abundance", hVPA_FAnC10:hVPA_FAnC99)
vf.cell.neg.nested <- vf.cell.neg.gathered %>% 
  group_by(Compound) %>% 
  nest() %>% 
  mutate(model = map(data, ~lm(Abundance ~ S1, data = .))) %>% 
  mutate(augment_model = map(model, augment))
vf.cell.neg.modSV.resid <- vf.cell.neg.nested %>% 
  unnest(data, augment_model) %>% 
  select(Samples, VPA, FA, Compound, .resid) %>% 
  spread(Compound, .resid)
vf.cell.neg.modSV.resid %>% 
  select(Samples:FA, one_of(unique(vf.cell.neg.hits$compound_short))) %>% 
  HeatmapPrepAlt("hVPA_FAnC") %>% 
  t() %>% 
  heatmaply(
    colors = viridis(n = 10, option = "magma"), 
    xlab = "Samples", ylab = "Compounds",
    main = "Statistically significant compounds\nVPA + FA HILIC / Cells / Neg Mode",
    margins = c(50, 50, 75, 30),
    k_col = 2, k_row = 2,
    labRow = unique(vf.cell.neg.hits$compound_full)
    )
### PCA - cleaned data ###
vf.cell.neg.modSV.pca <- vf.cell.neg.modSV.resid %>% 
  select(starts_with("hVPA_FAnC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
vf.cell.neg.modSV.pca.x <- as.data.frame(vf.cell.neg.modSV.pca$x)
row.names(vf.cell.neg.modSV.pca.x) <- vf.cell.neg.modSV.resid$Samples
vf.cell.neg.modSV.pca.x <- vf.cell.neg.modSV.pca.x %>% 
  bind_cols(vf.cell.neg.modSV.resid %>% select(VPA:FA))
vf.cell.neg.modSV.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (35.3% Var)") +
  ylab("PC2 (26.4% Var)")

vf.cell.neg.modSV.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (11.6% Var)") +
  ylab("PC4 (5.7% Var)")

4.2 Cells / Positive Mode

vf.cell.pos.smpl.data <- vf.cell.pos.noNA %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_")
vf.cell.pos.data.for.sva <- as.matrix(
  vf.cell.pos.smpl.data[, which(
    colnames(vf.cell.pos.smpl.data) == "hVPA_FApC1"
    ):ncol(vf.cell.pos.smpl.data)]
  )
row.names(vf.cell.pos.data.for.sva) <- vf.cell.pos.smpl.data$Samples
vf.cell.pos.data.for.sva <- t(vf.cell.pos.data.for.sva)
vf.cell.pos.data.pheno <- as.data.frame(vf.cell.pos.smpl.data[, 5:6])
row.names(vf.cell.pos.data.pheno) <- vf.cell.pos.smpl.data$Samples
vf.cell.pos.mod.vf <- model.matrix(~ 0 + as.factor(Treatment), data = vf.cell.pos.data.pheno)
vf.cell.pos.mod0 <- model.matrix(~ 1, data = vf.cell.pos.data.pheno)
set.seed(2018)
num.sv(vf.cell.pos.data.for.sva, vf.cell.pos.mod.vf, method = "be")
[1] 1
set.seed(2018)
num.sv(vf.cell.pos.data.for.sva, vf.cell.pos.mod.vf, method = "leek")
[1] 0
set.seed(2018)
vf.cell.pos.sv <- sva(vf.cell.pos.data.for.sva, vf.cell.pos.mod.vf, vf.cell.pos.mod0)
Number of significant surrogate variables is:  1 
Iteration (out of 5 ):1  2  3  4  5  
vf.cell.pos.surr.var <- as.data.frame(vf.cell.pos.sv$sv)
colnames(vf.cell.pos.surr.var) <- c("S1")
colnames(vf.cell.pos.mod.vf) <- c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
vf.cell.pos.design.sv <- cbind(vf.cell.pos.mod.vf, vf.cell.pos.surr.var)
vf.cell.pos.cont.mat <- makeContrasts(
  vpa_lowFA = vpa_only - cntrl, 
  vpa_highFA = fa_and_vpa - fa_only,
  FA_diff = (fa_and_vpa - fa_only) - (vpa_only - cntrl),
  levels = c("cntrl", "fa_only", "vpa_only", "fa_and_vpa", "S1")
  )
vf.cell.pos.eb <- vf.cell.pos.data.for.sva %>% 
  lmFit(vf.cell.pos.design.sv) %>% 
  contrasts.fit(vf.cell.pos.cont.mat) %>% 
  eBayes()
vf.cell.pos.eb.tidy <- tidy(vf.cell.pos.eb) %>% 
  mutate(
    adj_pval = p.adjust(p.value, method = "bonferroni"),
    FC = 2 ^ estimate,
    change_in_vpa = ifelse(FC < 1, "down", "up")
    ) %>% 
  rename(compound_short = gene)
vf.cell.pos.eb.tidy %>% 
  ggplot(aes(estimate, -log10(adj_pval))) +
  geom_point(size = 2, alpha = 0.5) +
  geom_hline(linetype = "dashed", color = "#009E73", yintercept = -log10(0.05)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1.2)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1/1.2)) +
  xlim(-2.5, 2.5) +
  xlab("log2(FC)") +
  ylab("-log10(adjusted p-value)") +
  ggtitle("Volcano plot\nVPA + FA / Cells / Positive Mode")

vf.cell.pos.hits <- vf.cell.pos.eb.tidy %>% 
  filter(adj_pval < 0.05 & (FC > 1.2 | FC < 1/1.2)) %>% 
  inner_join(vf.cell.pos.compound.info, by = "compound_short")
table(vf.cell.pos.hits$term)

   FA_diff vpa_highFA  vpa_lowFA 
         0          1          2 
sort(unique(vf.cell.pos.hits$compound_full))
[1] "D-Sorbitol" "Proline"   
vf.cell.pos.hits.tally2 <- vf.cell.pos.hits %>% 
  group_by(compound_short, compound_full) %>% 
  count() %>% 
  filter(n == 2)
vf.cell.pos.lowFA.hits <- vf.cell.pos.hits %>% 
  filter(term == "vpa_lowFA" & !(compound_short %in% vf.cell.pos.hits.tally2$compound_short))
vf.cell.pos.highFA.hits <- vf.cell.pos.hits %>% 
  filter(term == "vpa_highFA" & !(compound_short %in% vf.cell.pos.hits.tally2$compound_short))
vf.cell.pos.both.hits <- vf.cell.pos.hits %>% 
  filter(compound_short %in% vf.cell.pos.hits.tally2$compound_short) %>% 
  arrange(compound_short, term)

vf.cell.pos.hits
# A tibble: 3 x 14
  compound_short term  estimate statistic p.value   lod adj_pval    FC
  <chr>          <fct>    <dbl>     <dbl>   <dbl> <dbl>    <dbl> <dbl>
1 hVPA_FApC18    vpa_~     1.38      5.41 2.34e-5  2.78  0.00999 2.59 
2 hVPA_FApC69    vpa_~    -1.23     -4.97 6.45e-5  1.83  0.0275  0.425
3 hVPA_FApC18    vpa_~     1.74      5.86 8.28e-6  3.76  0.00353 3.33 
# ... with 6 more variables: change_in_vpa <chr>, compound_full <chr>,
#   formula <chr>, mass <dbl>, rt <dbl>, cas_id <chr>
### Plotting ###
vf.cell.pos.gathered <- vf.cell.pos.noNA %>%
  filter(Group == "sample") %>% 
  bind_cols(vf.cell.pos.surr.var) %>% 
  select(Samples, VPA, FA, S1, starts_with("hVPA_FApC")) %>% 
  gather(key = "Compound", value = "Abundance", hVPA_FApC1:hVPA_FApC99)
vf.cell.pos.nested <- vf.cell.pos.gathered %>% 
  group_by(Compound) %>% 
  nest() %>% 
  mutate(model = map(data, ~lm(Abundance ~ S1, data = .))) %>% 
  mutate(augment_model = map(model, augment))
vf.cell.pos.modSV.resid <- vf.cell.pos.nested %>% 
  unnest(data, augment_model) %>% 
  select(Samples, VPA, FA, Compound, .resid) %>% 
  spread(Compound, .resid)
vf.cell.pos.modSV.resid %>% 
  select(Samples:FA, one_of(unique(vf.cell.pos.hits$compound_short))) %>% 
  HeatmapPrepAlt("hVPA_FApC") %>% 
  t() %>% 
  heatmaply(
    colors = viridis(n = 10, option = "magma"), 
    xlab = "Samples", ylab = "Compounds",
    main = "Statistically significant compounds\nVPA + FA HILIC / Cells / Positive Mode",
    margins = c(50, 50, 75, 30),
    k_col = 2, k_row = 2,
    labRow = unique(vf.cell.pos.hits$compound_full)
    )
### PCA - cleaned data ###
vf.cell.pos.modSV.pca <- vf.cell.pos.modSV.resid %>% 
  select(starts_with("hVPA_FApC")) %>% 
  mutate_all(scale, center = TRUE, scale = FALSE) %>% 
  as.matrix() %>% 
  prcomp()
vf.cell.pos.modSV.pca.x <- as.data.frame(vf.cell.pos.modSV.pca$x)
row.names(vf.cell.pos.modSV.pca.x) <- vf.cell.pos.modSV.resid$Samples
vf.cell.pos.modSV.pca.x <- vf.cell.pos.modSV.pca.x %>% 
  bind_cols(vf.cell.pos.modSV.resid %>% select(VPA:FA))
vf.cell.pos.modSV.pca.x %>% 
  ggplot(aes(x = PC1, y = PC2, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC1 (66.4% Var)") +
  ylab("PC2 (12.5% Var)")

vf.cell.pos.modSV.pca.x %>% 
  ggplot(aes(x = PC3, y = PC4, color = VPA, shape = FA)) +
  geom_point(size = 4, alpha = 0.8) +
  xlab("PC3 (8.1% Var)") +
  ylab("PC4 (3.1% Var)")

4.3 Media / Negative Mode

# sample prep
vf.med.neg.smpl.data <- vf.med.neg.noNA %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_")
vf.med.neg.data.for.sva <- as.matrix(
  vf.med.neg.smpl.data[, which(
    colnames(vf.med.neg.smpl.data) == "hVPA_FAnM10"
    ):ncol(vf.med.neg.smpl.data)]
  )
row.names(vf.med.neg.data.for.sva) <- vf.med.neg.smpl.data$Samples
vf.med.neg.data.for.sva <- t(vf.med.neg.data.for.sva)
# pheno prep
vf.med.neg.data.pheno <- as.data.frame(vf.med.neg.smpl.data[, 5:6])
row.names(vf.med.neg.data.pheno) <- vf.med.neg.smpl.data$Samples
# sva calculation
vf.med.neg.mod.vf <- model.matrix(~ 0 + as.factor(Treatment), data = vf.med.neg.data.pheno)
vf.med.neg.mod0 <- model.matrix(~ 1, data = vf.med.neg.data.pheno)
set.seed(2018)
num.sv(vf.med.neg.data.for.sva, vf.med.neg.mod.vf, method = "be")
[1] 0
set.seed(2018)
num.sv(vf.med.neg.data.for.sva, vf.med.neg.mod.vf, method = "leek")
[1] 0
set.seed(2018)
vf.med.neg.sv <- sva(vf.med.neg.data.for.sva, vf.med.neg.mod.vf, vf.med.neg.mod0)
No significant surrogate variables
# extract the surrogate variables
colnames(vf.med.neg.mod.vf) <- c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
# combine the full model matrix and the surrogate variables into one
vf.med.neg.design.sv <- vf.med.neg.mod.vf
vf.med.neg.cont.mat <- makeContrasts(
  vpa_lowFA = vpa_only - cntrl, 
  vpa_highFA = fa_and_vpa - fa_only,
  FA_diff = (fa_and_vpa - fa_only) - (vpa_only - cntrl),
  levels = c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
  )
# fit the model/design matrix
vf.med.neg.eb <- vf.med.neg.data.for.sva %>% 
  lmFit(vf.med.neg.design.sv) %>% 
  contrasts.fit(vf.med.neg.cont.mat) %>% 
  eBayes()
# pull out the results for each metabolite for each comparison
vf.med.neg.eb.tidy <- tidy(vf.med.neg.eb) %>% 
  mutate(
    adj_pval = p.adjust(p.value, method = "bonferroni"),
    FC = 2 ^ estimate,
    change_in_vpa = ifelse(FC < 1, "down", "up")
    ) %>% 
  rename(compound_short = gene)
# volcano plot
vf.med.neg.eb.tidy %>% 
  ggplot(aes(estimate, -log10(adj_pval))) +
  geom_point(size = 2, alpha = 0.5) +
  geom_hline(linetype = "dashed", color = "#009E73", yintercept = -log10(0.05)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1.2)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1/1.2)) +
  xlab("log2(FC)") +
  ylab("-log10(adjusted p-value)") +
  ggtitle("Volcano plot\nVPA + FA / meds / Negative Mode")

# select statistically significant hits with a certain FC:
vf.med.neg.hits <- vf.med.neg.eb.tidy %>% 
  filter(adj_pval < 0.05 & (FC > 1.2 | FC < 1/1.2)) %>% 
  inner_join(vf.med.neg.compound.info, by = "compound_short")
# how many metabolites are significant across the different contrats
table(vf.med.neg.hits$term)

   FA_diff vpa_highFA  vpa_lowFA 
         0          2          1 
# significant metabolites
sort(unique(vf.med.neg.hits$compound_full))
[1] "Caprylic Acid" "Taurine"      
vf.med.neg.hits
# A tibble: 3 x 14
  compound_short term  estimate statistic  p.value   lod adj_pval    FC
  <chr>          <fct>    <dbl>     <dbl>    <dbl> <dbl>    <dbl> <dbl>
1 hVPA_FAnM21    vpa_~    5.48      69.1  5.78e-29 56.5  9.02e-27 44.8 
2 hVPA_FAnM14    vpa_~    0.519      6.47 1.12e- 6  3.97 1.74e- 4  1.43
3 hVPA_FAnM21    vpa_~    5.44      68.5  7.15e-29 56.3  1.12e-26 43.3 
# ... with 6 more variables: change_in_vpa <chr>, compound_full <chr>,
#   formula <chr>, mass <dbl>, rt <dbl>, cas_id <chr>

4.4 Media / Positive Mode

# sample prep
vf.med.pos.smpl.data <- vf.med.pos.noNA %>% 
  filter(Group == "sample") %>% 
  unite("Treatment", VPA:FA, sep = "_")
vf.med.pos.data.for.sva <- as.matrix(
  vf.med.pos.smpl.data[, which(
    colnames(vf.med.pos.smpl.data) == "hVPA_FApM1"
    ):ncol(vf.med.pos.smpl.data)]
  )
row.names(vf.med.pos.data.for.sva) <- vf.med.pos.smpl.data$Samples
vf.med.pos.data.for.sva <- t(vf.med.pos.data.for.sva)
vf.med.pos.data.pheno <- as.data.frame(vf.med.pos.smpl.data[, 5:6])
row.names(vf.med.pos.data.pheno) <- vf.med.pos.smpl.data$Samples
vf.med.pos.mod.vf <- model.matrix(~ 0 + as.factor(Treatment), data = vf.med.pos.data.pheno)
vf.med.pos.mod0 <- model.matrix(~ 1, data = vf.med.pos.data.pheno)
set.seed(2018)
num.sv(vf.med.pos.data.for.sva, vf.med.pos.mod.vf, method = "be")
[1] 0
set.seed(2018)
num.sv(vf.med.pos.data.for.sva, vf.med.pos.mod.vf, method = "leek")
[1] 0
set.seed(2018)
vf.med.pos.sv <- sva(vf.med.pos.data.for.sva, vf.med.pos.mod.vf, vf.med.pos.mod0)
No significant surrogate variables
colnames(vf.med.pos.mod.vf) <- c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
vf.med.pos.design.sv <- vf.med.pos.mod.vf
vf.med.pos.cont.mat <- makeContrasts(
  vpa_lowFA = vpa_only - cntrl, 
  vpa_highFA = fa_and_vpa - fa_only,
  FA_diff = (fa_and_vpa - fa_only) - (vpa_only - cntrl),
  levels = c("cntrl", "fa_only", "vpa_only", "fa_and_vpa")
  )
vf.med.pos.eb <- vf.med.pos.data.for.sva %>% 
  lmFit(vf.med.pos.design.sv) %>% 
  contrasts.fit(vf.med.pos.cont.mat) %>% 
  eBayes()
vf.med.pos.eb.tidy <- tidy(vf.med.pos.eb) %>% 
  mutate(
    adj_pval = p.adjust(p.value, method = "bonferroni"),
    FC = 2 ^ estimate,
    change_in_vpa = ifelse(FC < 1, "down", "up")
    ) %>% 
  rename(compound_short = gene)
# volcano plot
vf.med.pos.eb.tidy %>% 
  ggplot(aes(estimate, -log10(adj_pval))) +
  geom_point(size = 2, alpha = 0.5) +
  geom_hline(linetype = "dashed", color = "#009E73", yintercept = -log10(0.05)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1.2)) +
  geom_vline(linetype = "dashed", color = "#CC79A7", xintercept = log2(1/1.2)) +
  xlab("log2(FC)") +
  ylab("-log10(adjusted p-value)") +
  ggtitle("Volcano plot\nVPA + FA / meds / posative Mode")

# select statistically significant hits with a certain FC:
vf.med.pos.hits <- vf.med.pos.eb.tidy %>% 
  filter(adj_pval < 0.05 & (FC > 1.2 | FC < 1/1.2)) %>% 
  inner_join(vf.med.pos.compound.info, by = "compound_short")
# how many metabolites are significant across the different contrats
table(vf.med.pos.hits$term)

   FA_diff vpa_highFA  vpa_lowFA 
         0          0          0 

5 Session Info

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2      ggridges_0.5.1      biobroom_1.12.1    
 [4] broom_0.5.0         limma_3.36.5        sva_3.28.0         
 [7] BiocParallel_1.14.2 genefilter_1.62.0   mgcv_1.8-25        
[10] nlme_3.1-137        heatmaply_0.15.2    viridis_0.5.1      
[13] viridisLite_0.3.0   plotly_4.8.0        GGally_1.4.0       
[16] cowplot_0.9.3       forcats_0.3.0       stringr_1.3.1      
[19] dplyr_0.7.8         purrr_0.2.5         readr_1.1.1        
[22] tidyr_0.8.2         tibble_1.4.2        ggplot2_3.1.0      
[25] tidyverse_1.2.1    

loaded via a namespace (and not attached):
  [1] colorspace_1.3-2     class_7.3-14         modeltools_0.2-22   
  [4] mclust_5.4.1         rprojroot_1.3-2      rstudioapi_0.8      
  [7] flexmix_2.3-14       bit64_0.9-7          fansi_0.4.0         
 [10] AnnotationDbi_1.42.1 mvtnorm_1.0-8        lubridate_1.7.4     
 [13] xml2_1.2.0           splines_3.5.1        codetools_0.2-15    
 [16] robustbase_0.93-3    knitr_1.20           jsonlite_1.5        
 [19] annotate_1.58.0      cluster_2.0.7-1      kernlab_0.9-27      
 [22] shiny_1.2.0          compiler_3.5.1       httr_1.3.1          
 [25] backports_1.1.2      assertthat_0.2.0     Matrix_1.2-15       
 [28] lazyeval_0.2.1       cli_1.0.1            later_0.7.5         
 [31] htmltools_0.3.6      tools_3.5.1          gtable_0.2.0        
 [34] glue_1.3.0           reshape2_1.4.3       Rcpp_1.0.0          
 [37] Biobase_2.40.0       cellranger_1.1.0     trimcluster_0.1-2.1 
 [40] gdata_2.18.0         crosstalk_1.0.0      iterators_1.0.10    
 [43] fpc_2.1-11.1         rvest_0.3.2          mime_0.6            
 [46] gtools_3.8.1         XML_3.98-1.16        dendextend_1.9.0    
 [49] DEoptimR_1.0-8       MASS_7.3-51.1        scales_1.0.0        
 [52] TSP_1.1-6            promises_1.0.1       hms_0.4.2           
 [55] parallel_3.5.1       RColorBrewer_1.1-2   yaml_2.2.0          
 [58] memoise_1.1.0        gridExtra_2.3        reshape_0.8.8       
 [61] stringi_1.2.4        RSQLite_2.1.1        gclus_1.3.1         
 [64] S4Vectors_0.18.3     foreach_1.4.4        seriation_1.2-3     
 [67] caTools_1.17.1.1     BiocGenerics_0.26.0  matrixStats_0.54.0  
 [70] rlang_0.3.0.1        pkgconfig_2.0.2      prabclus_2.2-6      
 [73] bitops_1.0-6         evaluate_0.12        lattice_0.20-38     
 [76] bindr_0.1.1          labeling_0.3         htmlwidgets_1.3     
 [79] bit_1.1-14           tidyselect_0.2.5     plyr_1.8.4          
 [82] magrittr_1.5         R6_2.3.0             IRanges_2.14.12     
 [85] gplots_3.0.1         DBI_1.0.0            pillar_1.3.0        
 [88] haven_1.1.2          whisker_0.3-2        withr_2.1.2         
 [91] survival_2.43-1      RCurl_1.95-4.11      nnet_7.3-12         
 [94] modelr_0.1.2         crayon_1.3.4         utf8_1.1.4          
 [97] KernSmooth_2.23-15   rmarkdown_1.10       grid_3.5.1          
[100] readxl_1.1.0         data.table_1.11.8    blob_1.1.1          
[103] digest_0.6.18        diptest_0.75-7       webshot_0.5.1       
[106] xtable_1.8-3         httpuv_1.4.5         stats4_3.5.1        
[109] munsell_0.5.0        registry_0.5